#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :file_demo_v0.py
# @Time      :2023/6/30 
# @Author    :CL
# @email     :1037654919@qq.com

import json
import pandas as pd
import os
from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://cl007:ChengLei-0711@127.0.0.1:3306/test?charset=utf8")
import shutil

def readname(filePath):
    filePath = 'deviantart/'
    name = os.listdir(filePath)
    return name


if __name__ == "__main__":
    name = readname('deviantart/')
    # print(name)
    for i in name[:20]:
        print(i)

        save_path = 'deviantart/{}/'.format(i.split('.')[0])
        if os.path.exists(save_path):
            print('文件夹已经存在')
        else:
            os.mkdir(save_path)
        old_path ='deviantart/{}'.format(i)
        newpath = save_path+i
        shutil.copy(old_path,newpath)
        label_file = i.split('.')[0]+'.txt'
        sql = '''
                SELECT img_src, info_title, info_tag 
        FROM test.deviantart_image_infos
        WHERE img_src like "%{}%";
                '''.format(i)
        data = pd.read_sql(sql, engine)
        # print(len(data))
        if len(data)>0:
            txt_data = str(data['info_tag'][0])
            json_dict ={"page_url":data['img_src'][0],"images":[
                {
                    "img_file": i,
                    "label_file": label_file,
                    "tag": (data['info_tag'][0]),
                    "title": (data['info_title'][0]),
                    "description": ''
                }
            ]}
        else:
            txt_data='[]'
            json_dict = {"page_url":'',"images":[
                {
                    "img_file": i,
                    "label_file": label_file,
                    "tag": '',
                    "title": '',
                    "description": ''
                }
            ]}
        with open(save_path + label_file, 'w') as f:
            f.write(txt_data)
        f.close()
        with open(save_path + 'summary.json', 'w') as f2:
            f2.write(json.dumps(json_dict))
        f2.close()